//
//  NSString+Check.m
//  shuiYun
//
//  Created by aiquantong on 1/2/16.
//  Copyright © 2016 quantong. All rights reserved.
//

#import "NSString+Check.h"

@implementation  NSString(Check)

- (BOOL)isValidLetterAndNum
{
    NSString *regex = @"^[a-zA-Z0-9]+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES%@", regex];
    
    if ([predicate evaluateWithObject:self] == YES) {
        return YES;
    }
    return NO;
}

- (BOOL)isValidateEmail
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@", emailRegex];
    return [emailTest evaluateWithObject:self];
}

- (BOOL)isValidChinesePhone
{
    NSString *phoneRegex = @"1[0-9]{10}";
    NSPredicate *phoneCheck = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",phoneRegex];
    return [phoneCheck evaluateWithObject:self];
}

- (BOOL)isChineseCharacter
{
    BOOL isChinese = NO;
    NSString *chineseFormat = @"^[\u4e00-\u9fa5]+$";
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",chineseFormat];
    for (int i = 0; i < [self length]; i ++) {
        NSString *str = [self substringWithRange:NSMakeRange(i, 1)];
        if (str && [pre evaluateWithObject:str]) {
            isChinese = YES;
            break;
        }
    }
    return isChinese;
}

- (BOOL)isValidatePassword
{
    // length >8, can't only character or only numbers
    NSString *characterFormat = @"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,20}$";
    NSPredicate *characterCheck = [NSPredicate predicateWithFormat:@"SELF MATCHES%@", characterFormat];
    return [characterCheck evaluateWithObject:self];
}

@end
